import Vue from 'vue'
import VueRouter from 'vue-router'
import Main from "../views/main/Main";

const CategoryEdit = () => import('../views/category/CategoryEdit')
const CategoryList = () => import('../views/category/CategoryList')
const ArticleList  = () => import("../views/articles/ArticleList")
const ArticleEdit  = () => import("../views/articles/ArticleEdit")
const AdminUserEdit = () => import("../views/adminUser/AdminUserEdit")
const AdminUserList = () => import("../views/adminUser/AdminUserList")
const Login = () => import("../components/login/Login")
const Home = () => import('../views/main/Home')
const UserEdit = () => import('../views/user/UserEdit')
const UserList = () => import('../views/user/UserList')
const CommentEdit = () => import('../views/comment/CommentEdit')
const CommentList = () => import('../views/comment/CommentList')
const MessageEdit = ()=> import("../views/message/MessageEdit")
const MessageList = ()=> import("../views/message/MessageList")

Vue.use(VueRouter)

  const routes = [
    {
      path:'/',
      redirect:'/home',
      component:Main,
      children:[
        {path:'/home',component: Home},
        {path:'/categories/create',component: CategoryEdit},
        {path:'/categories/list',component: CategoryList},
        // 如果props 被设置为 true,route.params 将会被设置为组件属性
        {path:'/categories/edit/:id',component: CategoryEdit,props:true},
        {path:'/articles/create',component:ArticleEdit},
        {path:'/articles/list',component:ArticleList},
        {path:'/articles/edit/:id',component:ArticleEdit,props:true},
        {path:'/admin_users/create',component:AdminUserEdit},
        {path:'/admin_users/list',component:AdminUserList},
        {path:'/admin_users/edit/:id',component:AdminUserEdit,props:true},
        {path:'/users/create',component:UserEdit},
        {path:'/users/list',component:UserList},
        {path:'/users/edit/:id',component:UserEdit,props:true},
        {path:'/comments/create',component:CommentEdit},
        {path:'/comments/edit/:id',component:CommentEdit,props:true},
        {path:'/comments/list',component:CommentList},
        {path:'/message/create',component:MessageEdit},
        {path:'/message/list',component:MessageList},
        {path:'/message/edit/:id',component:MessageEdit,props:true}

      ]
     },
    {path: '/login',component: Login,meta:{isPublic:true}},
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    // component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  ]

const router = new VueRouter({
  routes
})
router.beforeEach((to,from,next)=>{
  console.log(to.meta)
  //如果不是公开访问的页面且页面的token不存在
  if(!to.meta.isPublic && !localStorage.getItem('token')){
    return next('/login')
  }
  next()
})
export default router
